<?php

use Biz\service\IntelligentRecommend\GoodThingRecommend;
use Biz\service\wiki\RecommendCardFormater;
use Biz\service\wiki\Wiki;
use Biz\service\wiki\WikiMiddle;
use Doraemon\service\position\Api;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzEnv;

/**
 * 好物频道推荐线
 * Class RecommendListAction
 */
class RecommendListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const PAGE_SIZE = 20;
    const IS_LOG_EXPOSE = 1;
    const IS_DEBUG = false;//debug 开关 上线关闭

    public function main()
    {
        $page = $this->getRequest()->getQuery('page', 0);
        $page = $page ? $page : 1;
        //查询推荐数据
        $list = GoodThingRecommend::getRecommendList($this->uid, $page);
        if (self::IS_DEBUG) {
            //debug 数据
            $debugList = [];
            if (HhzEnv::isGray()) {
                $debugList = $this->getGrayDebugList();
            } else if (HhzEnv::isTest()) {
                $debugList = $this->getHhcDebugList();
            }
            //$list = array_merge($list, $debugList);
            $list = $debugList;
            //debug 数据
        }

        if ($list) {
            if ($page == 1) {
                //轮播
                $carouselCard = Api::getTab4CarouselCard($this->uid);
                if ($carouselCard) {
                    $carouselCard = ['carouse_card' => ['obj_type' => BlockType::TYPE_CAROUSEL_BANNER, 'card_info' => $carouselCard]];
                    array_splice($list, 0, 0, $carouselCard);//第一位插入轮播banner
                }
                //资源位卡片
                $posCardList = Api::tab4Card($this->uid);
                if ($posCardList) {
                    $posIndexArr = array_column($posCardList, 'index');
                    array_multisort($posIndexArr, SORT_ASC, $posCardList);
                    foreach ($posCardList as $row) {
                        $index = $row['index'] - 1;
                        $posCard = ['carouse_card' => ['obj_type' => BlockType::TYPE_BANNER, 'card_info' => $row]];
                        array_splice($list, $index, 0, $posCard);
                    }
                }
            }
            $RecommendCardFormater = new RecommendCardFormater($list, $this->uid);
            $formated_list = $RecommendCardFormater->getFormatedList();
            $data['is_over'] = 0;
            $data['list'] = (isset($formated_list) && $formated_list && is_array($formated_list)) ? $formated_list : [];
        } else {
            $data = $this->getRecommendHot($page);
        }
        $this->setSuccess($data);
    }

    /**
     * wiki详情组装数据
     * @param $page
     * @return array
     */
    private function getRecommendHot($page)
    {
        $data = Wiki::getHotWikiList($this->uid, $page, self::PAGE_SIZE);
        $result = [];
        foreach ($data['list'] as $row) {
            $info['type'] = BlockType::TYPE_WIKI;

            $middleLink = WikiMiddle::getMiddleLink($row);

            $info['wiki'] = $row;
            $info['wiki']['middle_link'] = $middleLink;

            $info['statSign'] = '';
            if (isset($row['statSign'])) {
                $info['statSign'] = $row['statSign'];
            }
            $result[] = $info;
        }
        return ['list' => $result, 'is_over' => $data['is_over']];
    }

    /**
     * 好好测debug数据
     * @return array[]
     */
    private function getHhcDebugList()
    {
        $debugList = [
            //wiki
//            ['obj_type' => BlockType::TYPE_WIKI, 'obj_id' => '1739'],
//            ['obj_type' => BlockType::TYPE_WIKI, 'obj_id' => '1736'],
//            ['obj_type' => BlockType::TYPE_WIKI, 'obj_id' => '1720'],
            ['obj_type' => BlockType::TYPE_WIKI, 'obj_id' => '7363'],
            ['obj_type' => BlockType::TYPE_WIKI, 'obj_id' => '7354'],
            ['obj_type' => BlockType::TYPE_WIKI, 'obj_id' => '6729'],
            //note
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0009srr0000bolvm'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0009srs0000bolvm'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0009srt0000bolvm'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0009spq0000bomz6'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0009sqg0000bomz6'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0009sra0000bomz6'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0009sra0000bomz6'],
            //blank
            ['obj_type' => BlockType::TYPE_BLANK, 'obj_id' => '0000p7a0500bolun'],
            ['obj_type' => BlockType::TYPE_BLANK, 'obj_id' => '0000p790500bolun'],
            //article
            ['obj_type' => BlockType::TYPE_ARTICLE, 'obj_id' => '00010cc0100bolvm'],
            ['obj_type' => BlockType::TYPE_ARTICLE, 'obj_id' => '00010um0100bon5e'],
            ['obj_type' => BlockType::TYPE_ARTICLE, 'obj_id' => '0010uk0100bomz6'],
            ['obj_type' => BlockType::TYPE_ARTICLE, 'obj_id' => '00010ki0100bomz6'],
            //store
            ['obj_type' => BlockType::TYPE_STORE, 'obj_id' => 14],
            ['obj_type' => BlockType::TYPE_STORE, 'obj_id' => 15],
            ['obj_type' => BlockType::TYPE_STORE, 'obj_id' => 16],
            ['obj_type' => BlockType::TYPE_STORE, 'obj_id' => 20],

            //好物清单
            ['obj_type' => BlockType::TYPE_WIKI_ALBUM, 'obj_id' => '000003l12000097p'],
            ['obj_type' => BlockType::TYPE_WIKI_ALBUM, 'obj_id' => '000003m12000097p'],
            ['obj_type' => BlockType::TYPE_WIKI_ALBUM, 'obj_id' => '000003n12000097p'],
        ];
        return $debugList;
    }

    /**
     * gray debug 数据
     * @return array
     */
    private function getGrayDebugList()
    {
        $debugList = [
//            ['obj_type' => BlockType::TYPE_CATEGORY_WIKI, 'obj_id' => 812],
//            ['obj_type' => BlockType::TYPE_CATEGORY_WIKI, 'obj_id' => 811],
            //wiki
//            ['obj_type' => BlockType::TYPE_WIKI, 'obj_id' => '149833'],
//            ['obj_type' => BlockType::TYPE_WIKI, 'obj_id' => '145413'],
            //note
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0002d3h040000vit'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0014xdr00000fe84'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0015qph00000gott'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '001zah800000gott'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '003ivi700002pmwr'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0056lop00001vz21'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0057huj00006qzqj'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '005j5u10000135fs'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '005rtjm000018htb'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '005v0im00005itiq'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '005xckq00000pr6x'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0062txo00005j40g'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '00674a70000649xr'],

            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '00674a400004jzl3'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '00674a20000649xr'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '00674a100006pxbu'],
            ['obj_type' => BlockType::TYPE_PHOTO, 'obj_id' => '0067472000009add'],
            //blank
            ['obj_type' => BlockType::TYPE_BLANK, 'obj_id' => '0001qbt05003axsi'],
            ['obj_type' => BlockType::TYPE_BLANK, 'obj_id' => '0001qbo05000yyb1'],
            //article
            ['obj_type' => BlockType::TYPE_ARTICLE, 'obj_id' => '0003zie05005ho0r'],
            //store
//            ['obj_type' => BlockType::TYPE_STORE, 'obj_id' => 1],
//            ['obj_type' => BlockType::TYPE_STORE, 'obj_id' => 2],
//            ['obj_type' => BlockType::TYPE_STORE, 'obj_id' => 3],
//            ['obj_type' => BlockType::TYPE_STORE, 'obj_id' => 4],

            //好物清单
//            ['obj_type' => BlockType::TYPE_WIKI_ALBUM, 'obj_id' => '000002312000097p'],
//            ['obj_type' => BlockType::TYPE_WIKI_ALBUM, 'obj_id' => '000002412000097p'],
//            ['obj_type' => BlockType::TYPE_WIKI_ALBUM, 'obj_id' => '000002512000097p'],

        ];
        return $debugList;
    }
}